/**
 * 
 */
package com.netscenery.yang.designer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;

import com.netscenery.yang.model.Module;
/**
 * @author llly
 *
 */
public class ModuleLoad {
	private String name;
	private String path;
	private String fileName;
	private ArrayList<String> features;
	private String revisionDate;
	private ArrayList<ModuleLoad> subModules;
	private Module module;
	private File file;
	private ArrayList<Module> followingModules;
	private ArrayList<Module> followedModules;
	
	
	/**
	 * @return the file
	 */
	public File getFile() {
		return file;
	}
	/**
	 * @param file the file to set
	 */
	public void setFile(File file) {
		this.file = file;
	}
	
	/**
	 * @return the revisionDate
	 */
	public String getRevisionDate() {
		return revisionDate;
	}
	/**
	 * @param revisionDate the revisionDate to set
	 */
	public void setRevisionDate(String revisionDate) {
		this.revisionDate = revisionDate;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}
	/**
	 * @param path the path to set
	 */
	public void setPath(String path) {
		this.path = path;
	}
	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}
	/**
	 * @param fileName the fileName to set
	 */
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	/**
	 * @return the module
	 */
	public Module getModule() {
		return module;
	}
	/**
	 * @param module the module to set
	 */
	public void setModule(Module module) {
		this.module = module;
	}
	public boolean addFeature(String feature){
		if (null == feature){
			return false;
		}
		
		if (hasFeature(feature)){
			return false;
		}
		if(null == features){
			features = new ArrayList<String>();
		}
		
		return features.add(feature);
	}
	
	public String getFeature(int index){
		if (null == features){
			return null;
		}
		
		return features.get(index);
	}
	
	public boolean hasFeature(String feature){
		if (null == features){
			return false;
		}
		
		int size = features.size();
		for (int i = 0; i < size; i++){
			String f= features.get(i);
			if (null == f){
				continue;
			}
			
			if (f.equals(feature)){
				return true;
			}
		}
		
		return false;
	}
	
	public boolean removeFeature(String feature){
		if (null == features){
			return false;
		}
		
		int size = features.size();
		for (int i = 0; i < size; i++){
			String f= features.get(i);
			if (null == f){
				continue;
			}
			
			if (f.equals(feature)){
				features.remove(i);
				return true;
			}
		}
		
		return false;
	}
	public boolean removeFeature(int index){
		if (null == features){
			return false;
		}
		
		features.remove(index);
		return true;
	}
	public int getFeaturesNumber(){
		if (null == features){
			return 0;
		}
		
		return features.size();
	}
	
	public boolean addFollowingModule(Module module){
		if (null == module){
			return false;
		}
		
		if (null != getFollowingModule(module.getName(), module.getCurRevisionDate())){
			return false;
		}
		if(null == followingModules){
			followingModules = new ArrayList<Module>();
		}
		
		return followingModules.add(module);
	}
	
	public Module getFollowingModule(int index){
		if (null == followingModules){
			return null;
		}
		
		return followingModules.get(index);
	}
	
	public Module getFollowingModule(String ModuleName){
		if (null == followingModules){
			return null;
		}
		
		int size = followingModules.size();
		for (int i = 0; i < size; i++){
			Module module= followingModules.get(i);
			if (null == module){
				continue;
			}
			
			if (module.getName().equals(ModuleName)){
				return module;
			}
		}
		
		return module;
	}
	
	public Module getFollowingModule(String ModuleName,String revisionDate){
		if (null == followingModules){
			return null;
		}
		
		int size = followingModules.size();
		for (int i = 0; i < size; i++){
			Module module= followingModules.get(i);
			if (null == module){
				continue;
			}
			
			if (module.getName().equals(ModuleName)
					&& module.getCurRevisionDate().equals(revisionDate)){
				return module;
			}
		}
		
		return module;
	}
	
	public boolean removeFollowingModule(String moduleName){
		if (null == followingModules){
			return false;
		}
		int removeNumber = 0;
		int size = followingModules.size();
		for (int i = 0; i < size; i++){
			Module f= followingModules.get(i);
			if (null == f){
				continue;
			}
			
			if (f.getName().equals(moduleName)){
				followingModules.remove(i);
				removeNumber++;
			}
		}
		if (0 != removeNumber){
			return true;
		}
		return false;
	}
	
	public Module removeFollowingModule(String moduleName,String revisionDate){
		if (null == followingModules){
			return null;
		}
		int removeNumber = 0;
		int size = followingModules.size();
		for (int i = 0; i < size; i++){
			Module f= followingModules.get(i);
			if (null == f){
				continue;
			}
			
			if (f.getName().equals(moduleName)
					&& f.getCurRevisionDate().equals(revisionDate)){
				return followingModules.remove(i);
				
			}
		}
		
		return null;
	}
	public Module removeFollowingModule(int index){
		if (null == followingModules){
			return null;
		}
		
		return followingModules.remove(index);
	}
	public int getFollowingModulesNumber(){
		if (null == followingModules){
			return 0;
		}
		
		return followingModules.size();
	}
	public boolean addSubModule(ModuleLoad subModule){
		if (null == subModule){
			return false;
		}
		
		if (null != getSubModule(subModule.getName())){
			return false;
		}
		
		if (null == subModules){
			subModules = new ArrayList<ModuleLoad>();
		}
		
		return subModules.add(subModule);
	}
	
	public ModuleLoad getSubModule(String name){
		if (null == subModules){
			return null;
		}
		
		int size = subModules.size();
		
		for(int i = 0; i < size; i++){
			ModuleLoad sb= subModules.get(i);
			if (null == sb){
				continue;
			}
			
			if (sb.getName().equals(name)){
				return sb;
			}
		}
		
		return null;
	}
	
	public ModuleLoad getSubModule(int index){
		if (null == subModules){
			return null;
		}
		
		return subModules.get(index);
	}
	
	public ModuleLoad removeSubModule(String name){
		if (null == subModules){
			return null;
		}
		
		int size = subModules.size();
		
		for(int i = 0; i < size; i++){
			ModuleLoad sb= subModules.get(i);
			if (null == sb){
				continue;
			}
			
			if (sb.getName().equals(name)){
				return subModules.remove(i);
			}
		}
		
		return null;
	}
	
	public ModuleLoad removeSubModule(int index){
		if (null == subModules){
			return null;
		}
		
		return subModules.remove(index);
	}
	public int getSubModulesNumber(){
		if (null == subModules){
			return 0;
		}
		
		return subModules.size();
	}
	public boolean readXml(Element element){
		if (null == element){
			return false;
		}
		if (!(element.getName().equals("module")
				|| element.getName().equals("submodule"))){
			return false;
		}
		
		if (null != element.getChild("name")){
			this.setName(element.getChildText("name"));
		}
		if (null != element.getChild("path")){
			this.setPath(element.getChildText("path"));
		}
		
		if (null != element.getChild("filename")){
			this.setFileName(element.getChildText("filename"));
		}
		
		if (null != element.getChild("features")){
			Element featuresElement= element.getChild("features");
			List<Element> features= featuresElement.getChildren();
			if (null != features){
				int size = features.size();
				for (int i = 0; i < size; i++){
					Element feature= features.get(i);
					if (null == feature){
						continue;
					}
					
					this.addFeature(feature.getText());
				}
			}
		}
		
		if (null != element.getChild("submodules")){
			Element submodulesElement= element.getChild("submodules");
			List<Element> submodules= submodulesElement.getChildren();
			if (null != submodules){
				int size = submodules.size();
				for (int i = 0; i < size; i++){
					Element submodule= submodules.get(i);
					if (null == submodule){
						continue;
					}
					ModuleLoad sub = new ModuleLoad();
					sub.readXml(submodule);
					this.addSubModule(sub);
				}
			}
		}
		return true;
	}
	
	
}
